package com.gywj.race.util;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Map;

public class FreemarkerUtils {
    public static void geneFileStr(String path, String fileName, String templatePath, Map<String, Object> datas, String templateName) {

        Configuration cfg = new Configuration();
        try {
            cfg.setDirectoryForTemplateLoading(new File(templatePath));
            //加载具体模板文件，获得模板对象
            Template template = cfg.getTemplate(templateName);
            //编码格式
            cfg.setDefaultEncoding("utf-8");
            File pathFile = new File(path);
            if (!pathFile.exists()) {
                pathFile.mkdir();
            }
            File finalFile = new File(path + fileName);
            if (!finalFile.exists()) {
                finalFile.createNewFile();
            }
            Writer file = new FileWriter(finalFile);
            //生成文件
            template.process(datas, file);
            file.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
